package com.chenlehao.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

@Component
public class ObjToJSON {
    public static String objToJson(Object obj) throws JsonProcessingException {
        // 因为请求不通过需要返回JSON数据给客户端, 所以需要阿里巴巴提供的插件转换fastJSON
        // 创建ObjectMapper对象
        ObjectMapper objectMapper = new ObjectMapper();
        // 将Java对象转换为JSON字符串
        String json = objectMapper.writeValueAsString(obj);
        return json;
    }
}
